#include "painterrgb.h"
//#include "./Common/FrameTimestamp.h"
//#include <QPainter>

//PainterRGB::PainterRGB(QImage& img)
//    :m_rgbImg(img)
//{

//}

//void PainterRGB::fillRGB(uint8_t r, uint8_t g, uint8_t b)
//{
//    uint8_t arr[24] = {b, g, r, b, g, r, b, g, r, b, g, r, b, g, r, b, g, r, b, g, r, b, g, r};
//    int32_t ny = m_rgbImg.height();
//    uint64_t f0 = ((uint64_t*)arr)[0];
//    uint64_t f1 = ((uint64_t*)arr)[1];
//    uint64_t f2 = ((uint64_t*)arr)[2];

//    #pragma omp parallel for
//    for (int32_t y = 0; y < ny; ++y)
//    {
//        uint8_t* pix = (uint8_t*)m_rgbImg.scanLine(y);
//        uint8_t* pixend = pix + m_rgbImg.width() * 3 - 24;
//        while(pix < pixend)
//        {
//            ((uint64_t*)pix)[0] = f0;
//            ((uint64_t*)pix)[1] = f1;
//            ((uint64_t*)pix)[2] = f2;
//            pix += 24;
//        }
//        pixend += 24;
//        for (int32_t x = 0; pix < pixend; ++x)
//        {
//           *pix++ = arr[x];
//        }
//    }
//}

//QImage PainterRGB::scaleTo(int32_t w, int32_t h)
//{
//    QImage out(w, h, QImage::Format_RGB888);
//    ScalePos posx[w];
//    ScalePos posy[h];
//    makeScale(posx, m_rgbImg.width(), w, 3);
//    makeScale(posy, m_rgbImg.height(), h, m_rgbImg.bytesPerLine());

//    #pragma omp parallel for
//    for (int32_t y = 0; y < h; ++y)
//    {
//        ScalePos py = posy[y];
//        uint8_t* sline0 = m_rgbImg.bits() + py.i0;
//        uint8_t* sline1 = m_rgbImg.bits() + py.i1;
//        uint8_t* pix = out.scanLine(y);

//        for (int32_t x = 0; x < w ; ++x, pix += 3)
//        {
//            int ix = posx[x].i0;// stx - 0.5;
//            int vx = posx[x].v0;// 255 - (stx - ix) * 255;

//           // ix*= 3;
//            uint8_t* px00 = sline0 + ix;
//            uint8_t* px10 = sline1 + ix;

////            pix[0] = (((px00[0] - px00[3]) * vx + px00[3] * 255) * py.v0 + ((px10[0] - px10[3]) * vx + px10[3] * 255) * py.v1) >> 16;
////            pix[1] = (((px00[1] - px00[4]) * vx + px00[4] * 255) * py.v0 + ((px10[1] - px10[4]) * vx + px10[4] * 255) * py.v1) >> 16;
////            pix[2] = (((px00[2] - px00[5]) * vx + px00[5] * 255) * py.v0 + ((px10[2] - px10[5]) * vx + px10[5] * 255) * py.v1) >> 16;

//            pix[0] = ((px00[0] * vx + px00[3] * (255-vx)) * py.v0 + (px10[0] * vx + px00[3] * (255-vx)) * py.v1) >> 16;
//            pix[1] = ((px00[1] * vx + px00[4] * (255-vx)) * py.v0 + (px10[1] * vx + px00[4] * (255-vx)) * py.v1) >> 16;
//            pix[2] = ((px00[2] * vx + px00[5] * (255-vx)) * py.v0 + (px10[2] * vx + px00[5] * (255-vx)) * py.v1) >> 16;
//        }
//    }
//    return out;
//}

//void PainterRGB::makeScale(PainterRGB::ScalePos *pos, int32_t s, int32_t d, int32_t mulit)
//{
//    float dx = (float)s / d;
//    int32_t de = s - 1;
//    float st = dx * 0.5;
//    for (int x = 0; x < d; ++x)
//    {
//        float bg = st - 0.5;
//        if (bg <= 0.0f)
//        {
//            pos[x].i0 = 0;
//            pos[x].v0 = 255;
//            pos[x].i1 = 0;
//            pos[x].v1 = 0;
//        }
//        else if (bg >= de)
//        {
//            pos[x].i0 = de * mulit;
//            pos[x].v0 = 255;
//            pos[x].i1 = de * mulit;
//            pos[x].v1 = 0;
//        }
//        else
//        {
//            pos[x].i0 = int32_t(st) * mulit;
//            pos[x].v0 = (qCeil(bg) - bg) * 255;
//            pos[x].i1 = pos[x].i0 + mulit;
//            pos[x].v1 = 255 - pos[x].v0;
//        }
//        st += dx;
//    }
//}

//void testPainterRGB()
//{
////    FrameTimestamp tim;
////    int num = 100;
////    //QImage img(3840, 2160, QImage::Format_BGR888);
////    QImage img("/home/guee/图片/smplayer_screenshots/cap_00007_00:01:08_01.jpg");
////    img.convertTo(QImage::Format_BGR888);
////    QImage dst(2048, 1024, QImage::Format_BGR888);
////    PainterRGB rgb(img);


////    tim.start();
////    for ( int i = 0; i < num; ++i)
////    {
////        dst = rgb.scaleTo(dst.width(), dst.height());
////    }
////    qInfo() << "My Scale:" << float(tim.elapsed()) / (num * 1000) << "ms, fps:" << (1000000 * num) / tim.elapsed();

////    tim.start();
////    for ( int i = 0; i < num; ++i)
////    {
////        dst = img.scaled(dst.width(), dst.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
////    }
////    qInfo() << "Qt Scale:" << float(tim.elapsed()) / (num * 1000) << "ms, fps:" << (1000000 * num) / tim.elapsed();



////    QPainter pnt(&dst);
////    pnt.setRenderHint(QPainter::SmoothPixmapTransform);
////    tim.start();
////    for ( int i = 0; i < num; ++i)
////    {
////        pnt.drawImage(dst.rect(), img, img.rect());
////    }
////    qInfo() << "Qt draw:" << float(tim.elapsed()) / (num * 1000) << "ms, fps:" << (1000000 * num) / tim.elapsed();


////    tim.start();
////    for ( int i = 0; i < num; ++i)
////    {
////        rgb.fillRGB(122,141,42);
////    }
////    qInfo() << "My fill:" << float(tim.elapsed()) / (num * 1000) << "ms, fps:" << (1000000 * num) / tim.elapsed();

////    tim.start();
////    for ( int i = 0; i < num; ++i)
////    {
////        img.fill(QColor(122,141,42));
////    }
////    qInfo() << "Qt fill:" << float(tim.elapsed()) / (num * 1000) << "ms, fps:" << (1000000 * num) / tim.elapsed();

//}
